

package kr.ac.ssu.yoobh17.mPlayer.customView;


import kr.ac.ssu.yoobh17.mPlayer.GlobalClass;
import kr.ac.ssu.yoobh17.mPlayer.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;


public class LoopingButton extends FrameLayout implements OnTouchListener {

	private Button	button_looping;
	private int		loopingState	= GlobalClass.LOOPING_FALSE;

	//---------------------------------------------------------------------------------------------------

	private OnLoopingButtonClickListner loopingButtonClickListner = null;	

	public static interface OnLoopingButtonClickListner{
		
		public void onLoopingButtonClick( int loopingState );
		
	}

	public void setOnLoopingButtonClickListner( OnLoopingButtonClickListner loopingButtonClickListner ){

		this.loopingButtonClickListner = (OnLoopingButtonClickListner) loopingButtonClickListner;

	}

	//---------------------------------------------------------------------------------------------------
	
	public LoopingButton( Context context ) {

		this( context, null );
		// TODO Auto-generated constructor stub
	}

	public LoopingButton( Context context, AttributeSet attrs ) {

		super( context, attrs );
		// TODO Auto-generated constructor stub

		initView( context );

	}

	private void initView( Context context ) {

		// TODO Auto-generated method stub

		LayoutParams layoutParams = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT );

		button_looping = new Button( context );
		button_looping.setLayoutParams( layoutParams );
		button_looping.setOnTouchListener( this );
		
		addView( button_looping );

		button_looping.setBackgroundResource( R.drawable.image_looping_false );
		loopingState = GlobalClass.LOOPING_FALSE;

	}

	//---------------------------------------------------------------------------------------------------
	
	@Override
	public boolean onTouch( View view, MotionEvent event ) {

		// TODO Auto-generated method stub
		
		switch ( event.getAction() ) {
		
		case MotionEvent.ACTION_DOWN:

			if ( GlobalClass.LOOPING_FALSE == loopingState ) {

				button_looping.setBackgroundResource( R.drawable.image_looping_false_pressed );

			}
			else if( GlobalClass.LOOPING_TRUE == loopingState ){

				button_looping.setBackgroundResource( R.drawable.image_looping_true_pressed );

			}
			else{

				button_looping.setBackgroundResource( R.drawable.image_looping_one_pressed );

			}
			
			return true;

		case MotionEvent.ACTION_UP:

			toggleButton();
			
			if( null != loopingButtonClickListner ){
			
				loopingButtonClickListner.onLoopingButtonClick( loopingState );
			
			}

			return true;

		}
		
		return false;
	}

	private void toggleButton() {
		
		loopingState = ( loopingState + 1 ) % GlobalClass.NUM_OF_LOOPING_STATE;

		if ( GlobalClass.LOOPING_FALSE == loopingState ) {

			button_looping.setBackgroundResource( R.drawable.image_looping_false );

		}
		else if( GlobalClass.LOOPING_TRUE == loopingState ){

			button_looping.setBackgroundResource( R.drawable.image_looping_true );

		}
		else{

			button_looping.setBackgroundResource( R.drawable.image_looping_one );

		}
		
	}

	//---------------------------------------------------------------------------------------------------
	
	public void setState( int loopingState ) {

		this.loopingState = loopingState % GlobalClass.NUM_OF_LOOPING_STATE;

		if ( GlobalClass.LOOPING_FALSE == loopingState ) {

			button_looping.setBackgroundResource( R.drawable.image_looping_false );

		}
		else if( GlobalClass.LOOPING_TRUE == loopingState ){

			button_looping.setBackgroundResource( R.drawable.image_looping_true );

		}
		else{

			button_looping.setBackgroundResource( R.drawable.image_looping_one );

		}
		
	}

	public int getState() {

		return loopingState;
	}

}
